package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.model.entity.HVideoTraining;
import com.flea.hclv.model.entity.vo.HVideoVo;
import com.flea.hclv.service.HVideoTrainingService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @program: project
 * @description: 视频培训
 * @author: shiliqiang
 * @create: 2018-11-04 20:17
 * @version: 1.0
 */
@Controller
@RequestMapping("/flea/hclv/video/training")
public class HVideoTrainingController {

    @Autowired
    private HVideoTrainingService hVideoTrainingService;

    @RequestMapping("save")
    @ResponseBody
    public ServerResponse<String> save(HttpServletRequest request, HttpServletResponse response,String model){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
        if (dept==null){
            return ServerResponse.createByErrorMessage("部门为空");
        }
        if (StringUtils.isBlank(model)){
            return ServerResponse.createByErrorMessage("model为空");
        }
        return hVideoTrainingService.save(userId,dept.getDeptUuid(),model);
    }
    @RequestMapping("update")
    @ResponseBody
    public ServerResponse<String> update(HttpServletRequest request, HttpServletResponse response, String model){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        OrgDept dept = FleaOrgUtils.getCurrentUserDept(userId);
        if (dept==null){
            return ServerResponse.createByErrorMessage("没有找到该用户的部门信息");
        }
        HVideoTraining hVideoTraining = JSON.parseObject(model,HVideoTraining.class);
        return hVideoTrainingService.update(userId,hVideoTraining);
    }

    /**
     * 通过部门查找视频
     * @param request request
     * @param paging 分页对象
     * @return list
     */
    @RequestMapping("selectByDeptId")
    @ResponseBody
    public ServerResponse<FleaPage<HVideoVo>> selectByDeptId(HttpServletRequest request, FleaPage<HVideoTraining> paging,
                                                             String model){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        String deptId = FleaOrgUtils.getCurrentUserDeptUuid(request);
        return hVideoTrainingService.selectByDeptId(userId,paging.getCurPage(),paging.getPageSize(),model,deptId);
    }
    //所有视频
    @RequestMapping("selectAll")
    @ResponseBody
    public ServerResponse<FleaPage<HVideoVo>> selectAll(HttpServletRequest request, FleaPage<HVideoTraining> paging,
                                                             String model){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hVideoTrainingService.selectByDeptId(userId,paging.getCurPage(),paging.getPageSize(),model,null);
    }
    //通过产品id获取视频
    @RequestMapping("selectByProductId")
    @ResponseBody
    public ServerResponse<FleaPage<HVideoTraining>> selectByProductId(HttpServletRequest request, FleaPage<HVideoTraining> paging,
                                                        String productId){
        String userId = FleaOrgUtils.getCurUserUuid(request);
        return hVideoTrainingService.selectByProductId(productId,paging.getCurPage(),paging.getPageSize());
    }

    /**
     * 批量删除
     * @param ids  视频培训id
     * @return string
     */
    @RequestMapping("deleteByIds")
    @ResponseBody
    public ServerResponse<String> deleteByIds(HttpServletResponse response,String ids){
        List<String> idList= JSON.parseArray(ids,String.class);
        ServerResponse<String> serverResponse = hVideoTrainingService.deleteByIds(idList);
        return serverResponse;
    }
    //首页数据1
    @RequestMapping("selectHomeData")
    @ResponseBody
    public ServerResponse<Map<String,Object>> selectHomeData(HttpServletRequest request){
        ServerResponse<Map<String,Object>> serverResponse = hVideoTrainingService.selectHomeData();
        return serverResponse;
    }
    //首页数据2
    @RequestMapping("selectHomeDataTwo")
    @ResponseBody
    public ServerResponse<Map<String,Object>> selectHomeDataTwo(HttpServletRequest request){
        ServerResponse<Map<String,Object>> serverResponse = hVideoTrainingService.selectHomeDataTwo();
        return serverResponse;
    }
}
